package com.gduf.bbl.service.impl;

import com.gduf.bbl.common.utils.PageUtils;
import com.gduf.bbl.common.utils.Query;
import com.gduf.bbl.common.utils.R;
import com.gduf.bbl.dao.UserDao;
import com.gduf.bbl.entity.HelpListVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


import com.gduf.bbl.dao.HelplistDao;
import com.gduf.bbl.entity.HelplistEntity;
import com.gduf.bbl.service.HelplistService;

import javax.annotation.Resource;


@Service("heiplistService")
public class HelplistServiceImpl extends ServiceImpl<HelplistDao, HelplistEntity> implements HelplistService {
    @Resource
    private UserDao userDao;

    @Resource
    HelplistDao helplistDao;



    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<HelplistEntity> page = this.page(
                new Query<HelplistEntity>().getPage(params),
                new QueryWrapper<HelplistEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public R getListByUserId(Integer userId) {

        final QueryWrapper<HelplistEntity> helplistEntityQueryWrapper = new QueryWrapper<>();
        helplistEntityQueryWrapper.eq("user_id",userId);
        final List<HelplistEntity> helplistEntities = this.baseMapper.selectList(helplistEntityQueryWrapper);

        final List<HelpListVo> list = helplistEntities.stream().map((helplistEntity) -> {
            HelpListVo helpListVo = new HelpListVo();

            BeanUtils.copyProperties(helplistEntity, helpListVo);
            //设置用户头像
            String userImg = userDao.getUserImgByUserId(helplistEntity.getUserId());
            helpListVo.setUserImg(userImg);
            return helpListVo;
        }).collect(Collectors.toList());

        if(helplistEntities.size() > 0){
            return R.ok().setData(list);
        }else{
            return R.error();
        }
    }

    @Override
    public R getListByJdIdId(Integer userId) {
        final QueryWrapper<HelplistEntity> helplistEntityQueryWrapper = new QueryWrapper<>();
        helplistEntityQueryWrapper.eq("jd_id",userId);
        final List<HelplistEntity> helplistEntities = this.baseMapper.selectList(helplistEntityQueryWrapper);

        final List<HelpListVo> list = helplistEntities.stream().map((helplistEntity) -> {
            HelpListVo helpListVo = new HelpListVo();

            BeanUtils.copyProperties(helplistEntity, helpListVo);
            //设置用户头像
            String userImg = userDao.getUserImgByUserId(helplistEntity.getUserId());
            helpListVo.setUserImg(userImg);
            return helpListVo;
        }).collect(Collectors.toList());

        if(helplistEntities.size() > 0){
            return R.ok().setData(list);
        }else{
            return R.error();
        }
    }

    @Override
    public R getHelpListByAreaId(Integer areaId) {
        final QueryWrapper<HelplistEntity> helplistEntityQueryWrapper = new QueryWrapper<>();
        helplistEntityQueryWrapper.eq("area_id",areaId);
        final List<HelplistEntity> helplistEntities = this.baseMapper.selectList(helplistEntityQueryWrapper);


        final List<HelpListVo> list = helplistEntities.stream().map((helplistEntity) -> {
            HelpListVo helpListVo = new HelpListVo();

            BeanUtils.copyProperties(helplistEntity, helpListVo);
            //设置用户头像
            String userImg = userDao.getUserImgByUserId(helplistEntity.getUserId());
            helpListVo.setUserImg(userImg);
            return helpListVo;
        }).collect(Collectors.toList());

        if(helplistEntities.size() > 0){
            return R.ok().setData(list);
        }else{
            return R.error();
        }

    }

    @Override
    public boolean commitHelp2(HelplistEntity helplist) {
        return this.baseMapper.insert(helplist) > 0;
    }

    @Override
    public HelplistEntity getHelpListByOrderNo(String orderNo) {

        final QueryWrapper<HelplistEntity> helplistEntityQueryWrapper = new QueryWrapper<>();
        helplistEntityQueryWrapper.eq("order_no",orderNo);
        return this.baseMapper.selectOne(helplistEntityQueryWrapper);
    }

    @Override
    public void updateHelpStatus(String orderNo,String tradeNo) {
        helplistDao.updateHelpStatus(orderNo,tradeNo);
    }

}